package de.tabuma.ssoweb;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;
import javax.jdo.*;
import java.util.logging.*;
import java.util.*;
import de.tabuma.ssoweb.controller.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.4.1 $
 * @since $Date: 2010/01/13 10:23:45 $
 */
public class login extends HttpServlet {
    private static Logger log =  Logger.getLogger(login.class.getName());
    private controller maincontroller = new controller("login", new Hashtable());

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            UserService userService = UserServiceFactory.getUserService();
            if (request.getUserPrincipal() != null) {
                log.fine("User is logged in, so that everything can go it's normal why.");
                maincontroller.navigate(getServletContext(), request, response,out);
            } else {
                log.fine("User is not logged in, so login dialoge will be showed.");
                String thisURL = request.getRequestURI();
                Enumeration enumeration = request.getParameterNames();
                String addString = "";
                while(enumeration.hasMoreElements()) {
                    String key = (String) enumeration.nextElement();
                    String value = request.getParameter(key);
                    if(!addString.equals("")) {
                        addString += "&";
                    }
                    addString += key +"="+value;
                }
                thisURL = thisURL+"?"+addString;
                String loginUrl = userService.createLoginURL(thisURL);
                out.println("<html>");
                out.println("<script language=\"javascript\">");
                out.println("<!--");
                out.println("var weitergeleitet = \""+loginUrl+"\";");
                out.println("function weiterleitung() {");
                out.println("window.location = weitergeleitet ;");
                out.println("}");
                out.println("setTimeout(\"weiterleitung()\", 500);");
                out.println("//-->");
                out.println("</script>");
                out.println("<body>");
                out.println("If your browser does not support javascript: click at this <a href=\""+loginUrl+"\">login</a>");
                out.println("</body>");
                out.println("</html>");
            }
        }
        catch(Exception e) {
            String warning = "";
            warning += e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Servlet that handles all incoming parameter and given them to the central controller.";
    }// </editor-fold>

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        /**
         * In the following list there is the registration of Classes to enhancement
         */
        
        de.tabuma.customer.entity.Customer.class.getName();
        de.tabuma.customer.entity.CustomerUserRelation.class.getName();
        
        de.tabuma.sso.attribute.Attribute.class.getName();
        de.tabuma.sso.user.User.class.getName();
        de.tabuma.sso.usergroup.UserGroup.class.getName();
        de.tabuma.sso.useruserrelation.UserUserRelation.class.getName();
        de.tabuma.sso.group.Group.class.getName();
        de.tabuma.sso.groupprivileg.GroupPrivileg.class.getName();
        de.tabuma.sso.privileg.Privileg.class.getName();
        de.tabuma.sso.project.Project.class.getName();

        de.tabuma.shopping.entity.CurrentOrder.class.getName();
        de.tabuma.shopping.entity.CurrentOrderLicensePackageRelation.class.getName();
        de.tabuma.shopping.entity.License.class.getName();
        de.tabuma.shopping.entity.LicensePackage.class.getName();
        de.tabuma.shopping.entity.LicenseAssignment.class.getName();
        de.tabuma.shopping.entity.LicenseUse.class.getName();
        de.tabuma.shopping.entity.Order.class.getName();
        de.tabuma.shopping.entity.OrderLicensePackageRelation.class.getName();
        de.tabuma.shopping.entity.UserOrderRelation.class.getName();

        de.tabuma.logging.entity.LogEntry.class.getName();
        de.tabuma.logging.entity.LogField.class.getName();
        
    }
}
